package com.niiwoo.civet.mobile.lanmao.handler;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.niiwoo.civet.account.dto.request.BankCardRebindCallbackRequestDTO;
import com.niiwoo.civet.account.dto.response.BankCardRebindCallbackResponseDTO;
import com.niiwoo.civet.account.service.bankcard.BankCardDubboService;
import com.niiwoo.tripod.lanmao.enums.LanMaoGatewayServiceNameEnum;
import com.niiwoo.tripod.lanmao.notify.PersonalBindBankcardExpandNotify;
import com.niiwoo.tripod.provider.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 懒猫换绑卡回调
 * Created by wangjichao on 2017/11/21.
 */
@Slf4j
@Component
public class PersonalBindBankcardExpandNotifyHandler implements LanMaoNotifyHandler {

    @Reference(version = "1.0.0")
    private BankCardDubboService bankCardDubboService;


    @Override
    public boolean supports(String serviceName) {
        return LanMaoGatewayServiceNameEnum.PERSONAL_BIND_BANKCARD_EXPAND.name().equals(serviceName);
    }

    @Override
    public HandleResult handle(JSONObject json) {
        if (json == null) {
            log.error("银行卡换绑Handler,json is null");
        }
        log.info("银行卡换绑Handler,requestJson:{}", json.toJSONString());
        HandleResult handleResult = new HandleResult();
        handleResult.setNeedAck(false);
        PersonalBindBankcardExpandNotify notify = JSON.parseObject(json.toJSONString(), PersonalBindBankcardExpandNotify.class);
        BankCardRebindCallbackRequestDTO requestDTO = null;
        BankCardRebindCallbackResponseDTO responseDTO = null;
        //基本调用状态判断
        if (notify.getCode() == 0 && "SUCCESS".equals(notify.getStatus())) {
            requestDTO = new BankCardRebindCallbackRequestDTO();
            requestDTO.setBankAccountNo(notify.getBankcardNo());
            requestDTO.setAccountNo(notify.getPlatformUserNo());
            requestDTO.setMobileNo(notify.getMobile());
            requestDTO.setBankCode(notify.getBankcode());
            requestDTO.setRequestNo(notify.getRequestNo());
            responseDTO = bankCardDubboService.bankCardRebindCallback(requestDTO);
            handleResult.setRequestNo(responseDTO.getRequestNo());
            handleResult.setData(responseDTO);
            handleResult.setNeedAck(true);
            log.info("银行卡换绑Handler,handleResult:{}", JSON.toJSONString(handleResult));
            return handleResult;
        } else {
            throw new BizException(notify.getErrorCode(), notify.getErrorMessage());
        }
    }
}
